<template>
  <div id="app">
    <h1>TodoList</h1>
    <a-input
      placeholder="请输入任务"
      :value="inputVal"
      @change="handleInputChange"
      @keyup.enter="handleAddItem"
    />
    <a-button type="primary" @click="handleAddItem">添加事项</a-button>
    <!-- item-layout="vertical" -->
    <a-list item-layout="horizontal" :data-source="viewList" bordered>
      <a-list-item slot="renderItem" slot-scope="item, index">
        <!-- 复选框 -->
        <a-checkbox
          :checked="item.done"
          @change="handleCheckedChange(item, $event)"
          >{{ `${index + 1}、${item.info}` }}</a-checkbox
        >
        <!-- 删除链接 -->
        <a slot="actions" @click="handleRemoveItem(item)">删除</a>
      </a-list-item>
      <!-- footer区域 -->
      <div slot="footer" class="list-footer">
        <!-- 未完成的任务个数 -->
        <span>{{ unDoneLenth }}条剩余</span>
        <!-- 操作按钮 -->
        <a-button-group>
          <a-button :type="getViewType('all')" @click="setViewType('all')"
            >全部</a-button
          >
          <a-button :type="getViewType('undone')" @click="setViewType('undone')"
            >未完成</a-button
          >
          <a-button :type="getViewType('done')" @click="setViewType('done')"
            >已完成</a-button
          >
        </a-button-group>
        <!-- 把已经完成的任务清空 -->
        <a @click="removeCheckedItem">清除已完成</a>
      </div>
    </a-list>
  </div>
</template>

<script>
// mapMutations, mapActions, mapGetters
import { mapState, mapMutations, mapActions, mapGetters } from 'vuex'
export default {
  name: 'app',
  data() {
    return {}
  },
  created() {
    this.getList()
  },
  computed: {
    ...mapState(['inputVal', 'viewType']),
    ...mapGetters(['unDoneLenth', 'viewList'])
  },
  methods: {
    ...mapMutations([
      'setInputVal',
      'setViewType',
      'addItem',
      'removeItem',
      'removeCheckedItem',
      'updateItem'
    ]),
    ...mapActions(['getList']),
    handleInputChange(e) {
      // console.log(e.target.value.trim())
      this.setInputVal(e.target.value.trim())
    },
    handleAddItem() {
      if (this.inputVal.trim().length <= 0) {
        return this.$message.warning('请填写内容')
      }
      this.addItem()
    },
    handleRemoveItem(item) {
      this.removeItem(item.id)
    },
    handleCheckedChange(item, e) {
      // console.log(item, e.target.checked)
      this.updateItem({ id: item.id, done: e.target.checked })
    },
    getViewType(type) {
      if (type === this.viewType) {
        return 'primary'
      }
      return ''
    }
  }
}
</script>

<style scoped>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: #2c3e50;
  margin-top: 30px;
  padding: 0 10px;
}
.ant-input {
  width: 500px;
  margin-right: 10px;
}
.ant-list {
  width: 500px;
  margin-top: 10px;
}
.list-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>

<style>
.ant-spin-container {
  min-height: 166px;
}
</style>
